package cn.jbt.api.admin.service.impl;

import cn.jbt.api.admin.entity.QXxlJobGroup;
import cn.jbt.api.admin.entity.XxlJobGroup;
import cn.jbt.api.admin.service.IXxlJobGroupService;
import cn.jbt.common.PageQryParam;
import cn.jbt.common.PageQryResult;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import groovy.util.logging.Slf4j;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
@AllArgsConstructor
public class XxlJobGroupServiceImpl implements IXxlJobGroupService {

    private final JPAQueryFactory jpaQueryFactory;

    @Override
    public PageQryResult<XxlJobGroup> getAllByPage(PageQryParam<XxlJobGroup> pageQryParam) {
        XxlJobGroup qParam = pageQryParam.getParam();
        QXxlJobGroup qXxlJobGroup = QXxlJobGroup.xxlJobGroup;

        Predicate predicate = qXxlJobGroup.isNotNull().or(qXxlJobGroup.isNull());
        if (null != qParam) {
            if (StringUtils.isNotEmpty(qParam.getAppName())) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.appName.contains(qParam.getAppName()));
            }
            if (StringUtils.isNotEmpty(qParam.getTitle())) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.title.contains(qParam.getTitle()));
            }
            if (null != qParam.getAddressType()) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.addressType.eq(qParam.getAddressType()));
            }
            if (null != qParam.getCreateTimeStart()) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.updateTime.goe(qParam.getCreateTimeStart()));
            }
            if (null != qParam.getCreateTimeEnd()) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.updateTime.loe(qParam.getCreateTimeEnd()));
            }
        }

        List<XxlJobGroup> list = jpaQueryFactory.select(qXxlJobGroup)
                .from(qXxlJobGroup)
                .where(predicate)
                .orderBy(qXxlJobGroup.id.desc())
                .offset((pageQryParam.getPage() - 1) * pageQryParam.getSize())
                .limit(pageQryParam.getSize())
                .fetch();

        long total = Optional.ofNullable(jpaQueryFactory.select(qXxlJobGroup.id.count())
                .from(qXxlJobGroup)
                .where(predicate)
                .fetchOne()
        ).orElse(0L);

        return new PageQryResult<>(list, total);
    }

    @Override
    public List<XxlJobGroup> getAll(XxlJobGroup xxlJobGroup) {
        QXxlJobGroup qXxlJobGroup = QXxlJobGroup.xxlJobGroup;

        Predicate predicate = qXxlJobGroup.isNotNull().or(qXxlJobGroup.isNull());
        if (null != xxlJobGroup) {
            if (StringUtils.isNotEmpty(xxlJobGroup.getAppName())) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.appName.contains(xxlJobGroup.getAppName()));
            }
            if (StringUtils.isNotEmpty(xxlJobGroup.getTitle())) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.title.contains(xxlJobGroup.getTitle()));
            }
            if (null != xxlJobGroup.getAddressType()) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.addressType.eq(xxlJobGroup.getAddressType()));
            }
            if (null != xxlJobGroup.getCreateTimeStart()) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.updateTime.goe(xxlJobGroup.getCreateTimeStart()));
            }
            if (null != xxlJobGroup.getCreateTimeEnd()) {
                predicate = ExpressionUtils.and(predicate,
                        qXxlJobGroup.updateTime.loe(xxlJobGroup.getCreateTimeEnd()));
            }
        }

        return jpaQueryFactory.select(qXxlJobGroup)
                .from(qXxlJobGroup)
                .where(predicate)
                .orderBy(qXxlJobGroup.id.desc())
                .fetch();
    }

}
